package com.gr.smartedubackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gr.smartedubackend.common.BaseResponse;
import com.gr.smartedubackend.common.BusinessException;
import com.gr.smartedubackend.common.DeleteRequest;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.model.dto.student_group.StudentGroupAddRequest;
import com.gr.smartedubackend.model.dto.student_group.StudentGroupQueryByTeacherDto;
import com.gr.smartedubackend.model.dto.student_group.StudentGroupQueryRequest;
import com.gr.smartedubackend.model.dto.student_group.StudentGroupUpdateRequest;
import com.gr.smartedubackend.model.entity.StudentGroup;
import com.gr.smartedubackend.model.entity.Teacher;
import com.gr.smartedubackend.service.StudentGroupService;
import com.gr.smartedubackend.service.TeacherService;
import com.gr.smartedubackend.utils.ResultUtils;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/student_group")
public class StudentGroupController {

    @Resource
    private StudentGroupService studentGroupService;

    @Resource
    private TeacherService teacherService;

    @PostMapping("/add")
    public BaseResponse<Long> addStudentGroup(@RequestBody StudentGroupAddRequest studentGroupAddRequest) {
        ThrowUtils.throwIf(studentGroupAddRequest == null, ErrorCode.PARAMS_ERROR);
        Long teacherId = studentGroupAddRequest.getTeacherId();
        if (teacherId == null || teacherId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Teacher teacher = teacherService.getById(teacherId);
        if (teacher == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "教师不存在");
        }
        studentGroupAddRequest.setTeacherName(teacher.getTeacherName());
        StudentGroup studentGroup = new StudentGroup();
        BeanUtils.copyProperties(studentGroupAddRequest, studentGroup);
        boolean result = studentGroupService.save(studentGroup);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(studentGroup.getId());
    }

    @GetMapping("/get")
    public BaseResponse<StudentGroup> getStudentGroupById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        StudentGroup studentGroup = studentGroupService.getById(id);
        ThrowUtils.throwIf(studentGroup == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(studentGroup);
    }

    @PostMapping("/list/page")
    public BaseResponse<Page<StudentGroup>> listStudentGroupByPage(@RequestBody StudentGroupQueryRequest studentGroupQueryRequest) {
        ThrowUtils.throwIf(studentGroupQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int current = studentGroupQueryRequest.getCurrent();
        int pageSize = studentGroupQueryRequest.getPageSize();
        Page<StudentGroup> studentGroupPage = studentGroupService.page(new Page<>(current, pageSize),
                studentGroupService.getQueryWrapper(studentGroupQueryRequest));
        return ResultUtils.success(studentGroupPage);
    }

    @PostMapping("/list/page/byTeacher")
    public BaseResponse<Page<StudentGroup>> listStudentGroupByPageByTeacher(@RequestBody StudentGroupQueryByTeacherDto studentGroupQueryByTeacherDto) {
        ThrowUtils.throwIf(studentGroupQueryByTeacherDto == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(studentGroupService.listStudentGroupByPageByTeacher(studentGroupQueryByTeacherDto));
    }

    @PostMapping("/list")
    public BaseResponse<List<StudentGroup>> listStudentGroup(@RequestBody StudentGroupQueryRequest studentGroupQueryRequest) {
        ThrowUtils.throwIf(studentGroupQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(studentGroupService.list(studentGroupService.getQueryWrapper(studentGroupQueryRequest)));
    }


    @PostMapping("/update")
    public BaseResponse<Boolean> updateStudentGroup(@RequestBody StudentGroupUpdateRequest studentGroupUpdateRequest) {
        ThrowUtils.throwIf(studentGroupUpdateRequest == null || studentGroupUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        Long teacherId = studentGroupUpdateRequest.getTeacherId();
        ThrowUtils.throwIf(teacherId == null || teacherId <= 0, ErrorCode.PARAMS_ERROR);
        Teacher teacher = teacherService.getById(teacherId);
        ThrowUtils.throwIf(teacher == null, ErrorCode.NOT_FOUND_ERROR, "教师不存在");
        studentGroupUpdateRequest.setTeacherName(teacher.getTeacherName());
        StudentGroup studentGroup = new StudentGroup();
        BeanUtils.copyProperties(studentGroupUpdateRequest, studentGroup);
        boolean result = studentGroupService.updateById(studentGroup);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteStudentGroup(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        boolean result = studentGroupService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }
}
